package cn.dakaqi.services.impl;

import cn.dakaqi.dao.WelfareDao;
import cn.dakaqi.entities.Welfare;
import cn.dakaqi.services.WelfareService;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chunyang on 2016/4/18.
 */
@Component(value = "welfareService")
@Transactional
public class WelfareServiceImpl implements WelfareService
{
    @Autowired
    WelfareDao welfareDao;

    @Override
    public void save(String para) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(para);
        Welfare welfare = JSON.toJavaObject(jsonObject,Welfare.class);
        welfare.setCreateTime(Clock.DEFAULT.getCurrentDate());
        this.save(welfare);
    }

    @Override
    public void save(Welfare welfare) throws ServiceRuntimeException
    {
        this.welfareDao.save(welfare);
    }

    @Override
    public Page<Welfare> findAll(String area, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String,Object> map = new HashMap<String, Object>();
        //map.put("LIKE_NAME",name);
        //Map<String, SearchFilter> filters = SearchFilter.parse(map);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        if(StringUtils.isNotBlank(area))
            filters.put("area", new SearchFilter("area", SearchFilter.Operator.LIKE, area));

        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.WELFARE_STATUS_APPLY_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<Welfare> spec = DynamicSpecifications.bySearchFilter(filters.values(), Welfare.class);
        return this.welfareDao.findAll(spec, pageRequest);
    }

    @Override
    public Welfare findOne(Long id) throws ServiceRuntimeException
    {
        Welfare welfare = welfareDao.findOne(id);
        if(null != welfare)
        {
            welfare.setBrowses(welfare.getBrowses()+1);
            this.update(welfare);
        }
        return welfare;
    }

    @Override
    public void update(Welfare welfare) throws ServiceRuntimeException
    {
        this.welfareDao.save(welfare);
    }
}
